package cn.mesmile.pdf.entity;

import com.itextpdf.text.Document;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Date;

public class GeneratePDF2 {
 
    private static String USER_PASS = "123";
 
    private static String OWNER_PASS = "456";
 
    public static void main(String[] args) {
        try {
 
            OutputStream file = new FileOutputStream(new File("D:\\Test.pdf"));
 
            Document document = new Document();
            PdfWriter writer = PdfWriter.getInstance(document,file);
            // user密码是用户密码【用户密码，拥有所有权限】
            // owner密码是所有者密码，【“所有者密码”的PDF文件是不能被编辑和打印的】
            writer.setEncryption(USER_PASS.getBytes(),OWNER_PASS.getBytes(),PdfWriter.ALLOW_PRINTING,PdfWriter.ENCRYPTION_AES_128);

            document.open();
            document.add(new Paragraph("Hello World,iText"));
            document.add(new Paragraph(new Date().toString()));
 
            document.close();
            file.close();
 
        } catch (Exception e) {
 
            e.printStackTrace();
        }
    }
}